package com.corona.game.util;

import com.badlogic.gdx.Gdx;
import com.badlogic.gdx.files.FileHandle;
import com.badlogic.gdx.graphics.Pixmap;
import com.badlogic.gdx.graphics.PixmapIO;
import com.badlogic.gdx.utils.GdxRuntimeException;
import com.corona.game.PkmnMap;
import org.jetbrains.annotations.NotNull;
import org.jetbrains.annotations.Nullable;

/* loaded from: pokewilds.jar:com/pkmngen/game/util/Save.class */
public class Save {
    private static final String JSON_EXTENSION = "";
    private static final String ZIP_JSON_FILE = "";

    /* loaded from: pokewilds.jar:com/pkmngen/game/util/Save$Format.class */
    public enum Format {
        KRYO,
        PIXMAP,
        GSON
    }

    public static <T> void saveData(@NotNull T data, String path) throws SaveFailure {
        saveJson(data, path + JSON_EXTENSION);
    }

    /*  JADX ERROR: JadxRuntimeException in pass: BlockProcessor
        jadx.core.utils.exceptions.JadxRuntimeException: Unreachable block: B:6:0x0046
        	at jadx.core.dex.visitors.blocks.BlockProcessor.checkForUnreachableBlocks(BlockProcessor.java:86)
        	at jadx.core.dex.visitors.blocks.BlockProcessor.processBlocksTree(BlockProcessor.java:52)
        	at jadx.core.dex.visitors.blocks.BlockProcessor.visit(BlockProcessor.java:44)
        */
    private static <T> void saveJson(@NotNull T r7, String r8) throws SaveFailure {
        /*
            java.util.zip.ZipOutputStream r0 = new java.util.zip.ZipOutputStream     // Catch: IOException -> 0x0077
            r1 = r0
            java.io.BufferedOutputStream r2 = new java.io.BufferedOutputStream     // Catch: IOException -> 0x0077
            r3 = r2
            r4 = r8
            r5 = 0
            java.lang.String[] r5 = new java.lang.String[r5]     // Catch: IOException -> 0x0077
            java.nio.file.Path r4 = java.nio.file.Paths.get(r4, r5)     // Catch: IOException -> 0x0077
            r5 = 0
            java.nio.file.OpenOption[] r5 = new java.nio.file.OpenOption[r5]     // Catch: IOException -> 0x0077
            java.io.OutputStream r4 = java.nio.file.Files.newOutputStream(r4, r5)     // Catch: IOException -> 0x0077
            r3.<init>(r4)     // Catch: IOException -> 0x0077
            r1.<init>(r2)     // Catch: IOException -> 0x0077
            r9 = r0
            java.io.OutputStreamWriter r0 = new java.io.OutputStreamWriter     // Catch: Throwable -> 0x0062, IOException -> 0x0077
            r1 = r0
            r2 = r9
            java.nio.charset.Charset r3 = java.nio.charset.StandardCharsets.UTF_8     // Catch: Throwable -> 0x0062, IOException -> 0x0077
            r1.<init>(r2, r3)     // Catch: Throwable -> 0x0062, IOException -> 0x0077
            r10 = r0
            r0 = r9
            java.util.zip.ZipEntry r1 = new java.util.zip.ZipEntry     // Catch: Throwable -> 0x0062, Throwable -> 0x0046, IOException -> 0x0077
            r2 = r1
            java.lang.String r3 = "data.json"
            r2.<init>(r3)     // Catch: Throwable -> 0x0062, Throwable -> 0x0046, IOException -> 0x0077
            r0.putNextEntry(r1)     // Catch: Throwable -> 0x0062, Throwable -> 0x0046, IOException -> 0x0077
            com.google.gson.Gson r0 = com.corona.game.util.Json.gson     // Catch: Throwable -> 0x0062, Throwable -> 0x0046, IOException -> 0x0077
            r1 = r7
            r2 = r10
            r0.toJson(r1, r2)     // Catch: Throwable -> 0x0062, Throwable -> 0x0046, IOException -> 0x0077
            r0 = r10
            r0.close()     // Catch: Throwable -> 0x0062, IOException -> 0x0077
            goto L_0x005b
        L_0x0046:
            r11 = move-exception
            r0 = r10
            r0.close()     // Catch: Throwable -> 0x0062, IOException -> 0x0077, Throwable -> 0x004f
            goto L_0x0058
        L_0x004f:
            r12 = move-exception
            r0 = r11
            r1 = r12
            r0.addSuppressed(r1)     // Catch: Throwable -> 0x0062, IOException -> 0x0077
        L_0x0058:
            r0 = r11
            throw r0     // Catch: Throwable -> 0x0062, IOException -> 0x0077
        L_0x005b:
            r0 = r9
            r0.close()     // Catch: IOException -> 0x0077
            goto L_0x0074
        L_0x0062:
            r10 = move-exception
            r0 = r9
            r0.close()     // Catch: IOException -> 0x0077, Throwable -> 0x006a
            goto L_0x0072
        L_0x006a:
            r11 = move-exception
            r0 = r10
            r1 = r11
            r0.addSuppressed(r1)     // Catch: IOException -> 0x0077
        L_0x0072:
            r0 = r10
            throw r0     // Catch: IOException -> 0x0077
        L_0x0074:
            goto L_0x0085
        L_0x0077:
            r9 = move-exception
            com.corona.game.util.Save$SaveFailure r0 = new com.corona.game.util.Save$SaveFailure
            r1 = r0
            r2 = r9
            r3 = r8
            com.corona.game.util.Save$Format r4 = com.corona.game.util.Save.Format.GSON
            r1.<init>(r2, r3, r4)
            throw r0
        L_0x0085:
            return
        */
        throw new UnsupportedOperationException("Method not decompiled: com.corona.game.util.Save.saveJson(java.lang.Object, java.lang.String):void");
    }

    public static void saveMinimap(PkmnMap map, String path) throws SaveFailure {
        try {
            FileHandle file = new FileHandle(path);
            if (map.minimap == null) {
                map.minimap = new Pixmap(map.width(), map.height(), Pixmap.Format.RGBA8888);
                map.minimap.setColor(0.0f, 0.0f, 0.0f, 1.0f);
                map.minimap.fill();
            }
            PixmapIO.writePNG(file, map.minimap);
        } catch (GdxRuntimeException e) {
            throw new SaveFailure(e, path, Format.PIXMAP);
        }
    }

    @Nullable
    public static <T> T readDataIfPresent(String path, Class<T> type) throws ReadFailure {
        T data = (T) readJson(path + JSON_EXTENSION, type);
        if (data != null) {
            return data;
        }
        return (T) readKryo(path, type);
    }

    @NotNull
    public static <T> T readData(String path, Class<T> type) throws ReadFailure {
        T data = (T) readDataIfPresent(path, type);
        if (data != null) {
            return data;
        }
        throw ReadFailure.fileMissing(path);
    }

    /*  JADX ERROR: JadxRuntimeException in pass: BlockProcessor
        jadx.core.utils.exceptions.JadxRuntimeException: Unreachable block: B:12:0x0043
        	at jadx.core.dex.visitors.blocks.BlockProcessor.checkForUnreachableBlocks(BlockProcessor.java:86)
        	at jadx.core.dex.visitors.blocks.BlockProcessor.processBlocksTree(BlockProcessor.java:52)
        	at jadx.core.dex.visitors.blocks.BlockProcessor.visit(BlockProcessor.java:44)
        */
    @Nullable
    private static <T> T readJson(String r8, Class<T> r9) throws ReadFailure {
        /*
            r0 = r8
            boolean r0 = pathExists(r0)
            if (r0 != 0) goto L_0x0009
            r0 = 0
            return r0
        L_0x0009:
            java.util.zip.ZipFile r0 = new java.util.zip.ZipFile     // Catch: IOException -> 0x006a
            r1 = r0
            r2 = r8
            r1.<init>(r2)     // Catch: IOException -> 0x006a
            r10 = r0
            java.io.BufferedReader r0 = new java.io.BufferedReader     // Catch: IOException -> 0x006a, Throwable -> 0x0058
            r1 = r0
            java.io.InputStreamReader r2 = new java.io.InputStreamReader     // Catch: IOException -> 0x006a, Throwable -> 0x0058
            r3 = r2
            r4 = r10
            r5 = r10
            java.lang.String r6 = "data.json"
            java.util.zip.ZipEntry r5 = r5.getEntry(r6)     // Catch: IOException -> 0x006a, Throwable -> 0x0058
            java.io.InputStream r4 = r4.getInputStream(r5)     // Catch: IOException -> 0x006a, Throwable -> 0x0058
            java.nio.charset.Charset r5 = java.nio.charset.StandardCharsets.UTF_8     // Catch: IOException -> 0x006a, Throwable -> 0x0058
            r3.<init>(r4, r5)     // Catch: IOException -> 0x006a, Throwable -> 0x0058
            r1.<init>(r2)     // Catch: IOException -> 0x006a, Throwable -> 0x0058
            r11 = r0
            com.google.gson.Gson r0 = com.corona.game.util.Json.gson     // Catch: IOException -> 0x006a, Throwable -> 0x0058, Throwable -> 0x0043
            r1 = r11
            r2 = r9
            java.lang.Object r0 = r0.fromJson(r1, r2)     // Catch: IOException -> 0x006a, Throwable -> 0x0058, Throwable -> 0x0043
            r12 = r0
            r0 = r11
            r0.close()     // Catch: IOException -> 0x006a, Throwable -> 0x0058
            r0 = r10
            r0.close()     // Catch: IOException -> 0x006a
            r0 = r12
            return r0
        L_0x0043:
            r12 = move-exception
            r0 = r11
            r0.close()     // Catch: IOException -> 0x006a, Throwable -> 0x0058, Throwable -> 0x004c
            goto L_0x0055
        L_0x004c:
            r13 = move-exception
            r0 = r12
            r1 = r13
            r0.addSuppressed(r1)     // Catch: IOException -> 0x006a, Throwable -> 0x0058
        L_0x0055:
            r0 = r12
            throw r0     // Catch: IOException -> 0x006a, Throwable -> 0x0058
        L_0x0058:
            r11 = move-exception
            r0 = r10
            r0.close()     // Catch: IOException -> 0x006a, Throwable -> 0x0060
            goto L_0x0068
        L_0x0060:
            r12 = move-exception
            r0 = r11
            r1 = r12
            r0.addSuppressed(r1)     // Catch: IOException -> 0x006a
        L_0x0068:
            r0 = r11
            throw r0     // Catch: IOException -> 0x006a
        L_0x006a:
            r10 = move-exception
            r0 = r10
            r1 = r8
            com.corona.game.util.Save$Format r2 = com.corona.game.util.Save.Format.GSON
            com.corona.game.util.Save$ReadFailure r0 = com.corona.game.util.Save.ReadFailure.parsing(r0, r1, r2)
            throw r0
        */
        throw new UnsupportedOperationException("Method not decompiled: com.corona.game.util.Save.readJson(java.lang.String, java.lang.Class):java.lang.Object");
    }

    /*  JADX ERROR: JadxRuntimeException in pass: BlockProcessor
        jadx.core.utils.exceptions.JadxRuntimeException: Unreachable block: B:10:0x0042
        	at jadx.core.dex.visitors.blocks.BlockProcessor.checkForUnreachableBlocks(BlockProcessor.java:86)
        	at jadx.core.dex.visitors.blocks.BlockProcessor.processBlocksTree(BlockProcessor.java:52)
        	at jadx.core.dex.visitors.blocks.BlockProcessor.visit(BlockProcessor.java:44)
        */
    @Nullable
    private static <T> T readKryo(String r4, Class<T> r5) throws ReadFailure {
        /*
            r0 = r4
            boolean r0 = pathExists(r0)
            if (r0 != 0) goto L_0x0009
            r0 = 0
            return r0
        L_0x0009:
            com.badlogic.gdx.Files r0 = com.badlogic.gdx.Gdx.files     // Catch: Exception -> 0x0058
            r1 = r4
            com.badlogic.gdx.files.FileHandle r0 = r0.local(r1)     // Catch: Exception -> 0x0058
            r6 = r0
            java.util.zip.InflaterInputStream r0 = new java.util.zip.InflaterInputStream     // Catch: Exception -> 0x0058
            r1 = r0
            r2 = r6
            java.io.InputStream r2 = r2.read()     // Catch: Exception -> 0x0058
            r1.<init>(r2)     // Catch: Exception -> 0x0058
            r7 = r0
            com.esotericsoftware.kryo.io.Input r0 = new com.esotericsoftware.kryo.io.Input     // Catch: Exception -> 0x0058
            r1 = r0
            r2 = r7
            r1.<init>(r2)     // Catch: Exception -> 0x0058
            r8 = r0
            com.corona.Game r0 = com.corona.Game.staticGame     // Catch: Exception -> 0x0058, Throwable -> 0x0042
            com.esotericsoftware.kryonet.Server r0 = r0.server     // Catch: Exception -> 0x0058, Throwable -> 0x0042
            com.esotericsoftware.kryo.Kryo r0 = r0.getKryo()     // Catch: Exception -> 0x0058, Throwable -> 0x0042
            r1 = r8
            r2 = r5
            java.lang.Object r0 = r0.readObject(r1, r2)     // Catch: Exception -> 0x0058, Throwable -> 0x0042
            r9 = r0
            r0 = r8
            r0.close()     // Catch: Exception -> 0x0058
            r0 = r9
            return r0
        L_0x0042:
            r9 = move-exception
            r0 = r8
            r0.close()     // Catch: Exception -> 0x0058, Throwable -> 0x004c
            goto L_0x0055
        L_0x004c:
            r10 = move-exception
            r0 = r9
            r1 = r10
            r0.addSuppressed(r1)     // Catch: Exception -> 0x0058
        L_0x0055:
            r0 = r9
            throw r0     // Catch: Exception -> 0x0058
        L_0x0058:
            r6 = move-exception
            r0 = r6
            r1 = r4
            com.corona.game.util.Save$Format r2 = com.corona.game.util.Save.Format.KRYO
            com.corona.game.util.Save$ReadFailure r0 = com.corona.game.util.Save.ReadFailure.parsing(r0, r1, r2)
            throw r0
        */
        throw new UnsupportedOperationException("Method not decompiled: com.corona.game.util.Save.readKryo(java.lang.String, java.lang.Class):java.lang.Object");
    }

    public static boolean isDataPresent(String path) {
        return isJsonPresent(path) || isKryoPresent(path);
    }

    private static boolean isJsonPresent(String path) {
        return pathExists(path + JSON_EXTENSION);
    }

    private static boolean isKryoPresent(String path) {
        return pathExists(path);
    }

    private static boolean pathExists(String path) {
        return Gdx.files.local(path).exists();
    }

    /* loaded from: pokewilds.jar:com/pkmngen/game/util/Save$SaveFailure.class */
    public static class SaveFailure extends Exception {
        private final String path;
        private final Format format;

        public SaveFailure(Throwable cause, String path, Format format) {
            super(cause);
            this.path = path;
            this.format = format;
        }

        @Override // java.lang.Throwable
        public String getMessage() {
            return "<" + this.format + "> Failed to save file " + this.path + ":\n" + getMessage();
        }
    }

    /* loaded from: pokewilds.jar:com/pkmngen/game/util/Save$ReadFailure.class */
    public static class ReadFailure extends Exception {
        private ReadFailure(String message, Throwable cause) {
            super(message, cause);
        }

        public ReadFailure(String message) {
            super(message);
        }

        public static ReadFailure parsing(Throwable cause, String path, Format format) {
            return new ReadFailure("<" + format + "> Failed to read file: " + path, cause);
        }

        public static ReadFailure fileMissing(String path) {
            return new ReadFailure("No file found: " + path);
        }
    }
}
